#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
辅食数据库初始化脚本
运行此脚本来创建辅食相关的数据库表并插入初始数据
"""

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import Config
from models import FoodCategory, FoodOption
from db import db

# 创建独立的Flask应用实例
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)

def init_complementary_food_data():
    """初始化辅食数据"""
    with app.app_context():
        # 创建所有表
        db.create_all()
        
        # 清空现有数据（按外键依赖顺序删除）
        from models import ComplementaryFoodDetail, ComplementaryFoodRecord
        ComplementaryFoodDetail.query.delete()
        ComplementaryFoodRecord.query.delete()
        FoodOption.query.delete()
        FoodCategory.query.delete()
        db.session.commit()
        print("已清空现有辅食数据")
        
        # 创建五大类别
        categories_data = [
            {"name": "主食"},
            {"name": "蔬菜"},
            {"name": "肉"},
            {"name": "海鲜"},
            {"name": "水果"}
        ]
        
        categories = {}
        for cat_data in categories_data:
            category = FoodCategory(name=cat_data["name"])
            db.session.add(category)
            db.session.flush()  # 获取ID
            categories[cat_data["name"]] = category.id
        
        # 创建常见食物选项
        food_options_data = [
            # 主食类
            {"name": "米粉", "category": "主食"},
            {"name": "小米粥", "category": "主食"},
            {"name": "燕麦粥", "category": "主食"},
            {"name": "面条", "category": "主食"},
            {"name": "馒头", "category": "主食"},
            {"name": "面包", "category": "主食"},
            {"name": "红薯", "category": "主食"},
            {"name": "土豆", "category": "主食"},
            
            # 蔬菜类
            {"name": "胡萝卜", "category": "蔬菜"},
            {"name": "南瓜", "category": "蔬菜"},
            {"name": "西兰花", "category": "蔬菜"},
            {"name": "菠菜", "category": "蔬菜"},
            {"name": "白菜", "category": "蔬菜"},
            {"name": "豌豆", "category": "蔬菜"},
            {"name": "玉米", "category": "蔬菜"},
            {"name": "冬瓜", "category": "蔬菜"},
            {"name": "茄子", "category": "蔬菜"},
            {"name": "黄瓜", "category": "蔬菜"},
            
            # 肉类
            {"name": "鸡肉", "category": "肉"},
            {"name": "猪肉", "category": "肉"},
            {"name": "牛肉", "category": "肉"},
            {"name": "鸡蛋", "category": "肉"},
            {"name": "鹌鹑蛋", "category": "肉"},
            
            # 海鲜类
            {"name": "三文鱼", "category": "海鲜"},
            {"name": "鳕鱼", "category": "海鲜"},
            {"name": "虾", "category": "海鲜"},
            {"name": "扇贝", "category": "海鲜"},
            {"name": "紫菜", "category": "海鲜"},
            
            # 水果类
            {"name": "苹果", "category": "水果"},
            {"name": "香蕉", "category": "水果"},
            {"name": "梨", "category": "水果"},
            {"name": "橙子", "category": "水果"},
            {"name": "葡萄", "category": "水果"},
            {"name": "草莓", "category": "水果"},
            {"name": "蓝莓", "category": "水果"},
            {"name": "猕猴桃", "category": "水果"},
            {"name": "芒果", "category": "水果"},
            {"name": "火龙果", "category": "水果"}
        ]
        
        for food_data in food_options_data:
            food_option = FoodOption(
                name=food_data["name"],
                category_id=categories[food_data["category"]],
                is_custom=False
            )
            db.session.add(food_option)
        
        # 提交所有更改
        db.session.commit()
        print("辅食数据初始化完成！")
        print(f"创建了 {len(categories_data)} 个食物类别")
        print(f"创建了 {len(food_options_data)} 个常见食物选项")

if __name__ == "__main__":
    init_complementary_food_data()